from collections import Counter
from typing import List


class Solution:
    def subarraySum(self, nums: List[int], k: int) -> int:
        count = Counter({0: 1})
        prefix = 0
        ans = 0
        for num in nums:
            prefix += num
            ans += count[prefix - k]
            count[prefix] += 1
        return ans


if __name__ == "__main__":
    print(Solution().subarraySum(nums=[1, 1, 1], k=2))  # 2
    print(Solution().subarraySum(nums=[1, 2, 3], k=3))  # 2
    print(Solution().subarraySum(nums=[1], k=0))  # 0
